package com.LeeCode;

import java.util.Arrays;

/**
 * 找到和最大的长度为 K 的子序列
 */

public class Code2099 {
    public static void main(String[] args) {
        int[] nums = {-1, -2, 3, 4};
        int k = 3;
        System.out.println(Arrays.toString(new Code2099().maxSubsequence(nums, k)));
    }

    public int[] maxSubsequence(int[] nums, int k) {
        Integer[] arr = new Integer[nums.length];
        Arrays.setAll(arr, i -> i);

        Arrays.sort(arr, (a, b) -> {
            return nums[a] - nums[b];
        });
        int[] ans = new int[k];
        int n = nums.length;
        for (int i = 0; i < k; i++) {
            ans[i] = arr[n - 1 - i];
        }
        Arrays.sort(ans);
        for (int i = 0; i < k; i++) {
            ans[i] = nums[ans[i]];
        }
        return ans;
    }
}
